-- SERVER SIDE



-- Creates the Database
TDMAppleStats = "apple_tdm_stats5"
if sql.TableExists(TDMAppleStats) then
--MsgN("[TDM-Maps] Database 'apple_tdm_map' Already Exists!")
MsgC(Color(255,255,255),"[",Color(255,0,0),"TDM-StatsDB",Color(255,255,255),"] Database '"..TDMAppleStats.."' Already Exists!\n")
	else
		if (!sql.TableExists(TDMAppleStats)) then
			local Query = "CREATE TABLE "..TDMAppleStats.." ( ID int, PID int, Name varchar(255), Time varchar(255), Kills varchar(255), Deaths varchar(255) )"
			local Result = sql.Query(Query)
			if (sql.TableExists(TDMAppleStats)) then
				--MsgN("[TDM-Maps] Successfully Created Database 'apple_tdm_stats'!")
				MsgC(Color(255,255,255),"[",Color(255,0,0),"TDM-StatsDB",Color(255,255,255),"] Successfully Created Database '"..TDMAppleStats.."'!\n")
			else
				--MsgN("[TDM-Maps] Database 'apple_tdm_stats' Was Not Created!")
				MsgC(Color(255,255,255),"[",Color(255,0,0),"TDM-StatsDB",Color(255,255,255),"] Database '"..TDMAppleStats.."' Was Not Created!\n")
				MsgN( sql.LastError( Result ) )
			end	
		end
end




function PlayerInitialStatsTDMC(ply)
local GetLastPlayerIdC = sql.Query( "SELECT * FROM "..TDMAppleStats.." WHERE PID = "..tostring(ply:UniqueID()).."" )
if GetLastPlayerIdC then 
	--MsgN("[TDM-ChallengeP] Player Already Exists!")
	MsgC(Color(255,255,255),"[",Color(255,0,0),"TDM-StatsAdd",Color(255,255,255),"] Player Already Exists!\n")
	for k, v in pairs( GetLastPlayerIdC ) do
		timer.Simple(2, function()
		if !ply:IsValid() then return end
			umsg.Start( "TDMStatsKill", ply )
				umsg.String(v['Kills'])
			umsg.End()
			umsg.Start( "TDMStatsDeaths", ply )
				umsg.String(v['Deaths'])
			umsg.End()
			umsg.Start( "TDMStatsTime", ply )
				umsg.String(v['Time'])
			umsg.End()
		end)
	end
return end

local LastStatsIDCreated2 = tonumber(sql.QueryValue( "SELECT ID FROM "..TDMAppleStats.." ORDER BY ID DESC LIMIT 1" ))
if LastStatsIDCreated2 == nil then
	LastStatsIDCreated2 = 0
end

local LastStatsIDCreated2 = (LastStatsIDCreated2 + 1)
	--MsgN("[TDM-ChallengeP] ChallengeP Was Created Successfully, PlayerID: "..LastStatsIDCreated2)
	MsgC(Color(255,255,255),"[",Color(255,0,0),"TDM-ChallengeAdd",Color(255,255,255),"] ChallengeP Was Created Successfully, PlayerID: ",Color(0,255,0),""..LastStatsIDCreated2.."\n")
	sql.Query( "INSERT INTO "..TDMAppleStats.." (`ID`, `PID`, `Name`, `Time`, `Kills`, `Deaths`) VALUES ('"..tonumber(LastStatsIDCreated2).."', '"..tostring(ply:UniqueID()).."', '"..tostring(ply:Nick()).."', '0', '0', '0') ")
timer.Simple(2, function()
if !ply:IsValid() then return end
	umsg.Start( "TDMStatsKill", ply )
		umsg.String("0")
	umsg.End()
	umsg.Start( "TDMStatsDeaths", ply )
		umsg.String("0")
	umsg.End()
end)
end
hook.Add( "PlayerInitialSpawn", "PlayerInitialStatsTDMC", PlayerInitialStatsTDMC )


function updateStatsAll()
	local players = player.GetAll()
	for _, ply in ipairs( players ) do
		if ply and ply:IsValid() and ply:IsConnected() then
			local GetTimeStats = sql.Query( "SELECT * FROM "..TDMAppleStats.." WHERE PID = "..ply:UniqueID().."" )
			if !GetTimeStats then 
					MsgC(Color(255,255,255),"[",Color(255,0,0),"TDM-StatsUpdate",Color(255,255,255),"] Player Does Not Exist!\n")
				
				else
				
				for k, v in pairs( GetTimeStats ) do
				local GetMathTSt = tonumber(v['Time']) + 1
					sql.Query( "UPDATE "..TDMAppleStats.." SET Time = "..GetMathTSt.." WHERE PID = "..ply:UniqueID().."" )
					
					umsg.Start( "TDMStatsTime", ply )
						umsg.String(GetMathTSt)
					umsg.End()
				end
			end
		end
	end
end
timer.Create( "UpdateStatsTimer", 60, 0, updateStatsAll )


function PlayerDeathUpdateStats(vic,wep,kil)
if !kil:IsPlayer() || !vic:IsPlayer() then return end
if vic:Team() == kil:Team() then return end
if kil == vic then return end


local GetDeathStats = sql.Query( "SELECT * FROM "..TDMAppleStats.." WHERE PID = "..vic:UniqueID().."" )
if !GetDeathStats then 
		MsgC(Color(255,255,255),"[",Color(255,0,0),"TDM-StatsUpdate",Color(255,255,255),"] Player Does Not Exist!\n")
	
	else
	
	for k, v in pairs( GetDeathStats ) do
		local GetDeathSt = tonumber(v['Deaths']) + 1
		sql.Query( "UPDATE "..TDMAppleStats.." SET Deaths = "..GetDeathSt.." WHERE PID = "..vic:UniqueID().."" )
		
		umsg.Start( "TDMStatsDeaths", vic )
			umsg.String(GetDeathSt)
		umsg.End()
	end
end


local GetKillersStats = sql.Query( "SELECT * FROM "..TDMAppleStats.." WHERE PID = "..kil:UniqueID().."" )
if !GetKillersStats then 
		MsgC(Color(255,255,255),"[",Color(255,0,0),"TDM-StatsUpdate",Color(255,255,255),"] Player Does Not Exist!\n")
	
	else
	
	for k, v in pairs( GetKillersStats ) do
		local GetKillsSt = tonumber(v['Kills']) + 1
		sql.Query( "UPDATE "..TDMAppleStats.." SET Kills = "..GetKillsSt.." WHERE PID = "..kil:UniqueID().."" )
		
		umsg.Start( "TDMStatsKill", kil )
			umsg.String(GetKillsSt)
		umsg.End()
	end
end
end
hook.Add( "PlayerDeath", "PlayerDeathUpdateStats", PlayerDeathUpdateStats )